package cn.group01.git.service.impl;

import cn.group01.git.common.CodeMsg;
import cn.group01.git.common.ResponseResult;
import cn.group01.git.dto.UserPageDTO;
import cn.group01.git.entity.User;
import cn.group01.git.form.LoginForm;
import cn.group01.git.mapper.UserMapper;
import cn.group01.git.service.UserService;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @description:
 * @author: 吴凡荣
 * @date: 2023/7/13 11:14
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public ResponseResult register(User user) {
        User usr = userMapper.getByUsername(user.getUsername());// 查询用户是否存在
        if(ObjectUtils.isEmpty(usr)){ // 如果该用户为空
            String md5 = SecureUtil.md5(user.getPassword());// 密码加密
            user.setPassword(md5);
            userMapper.register(user); // 调用注册方法 如果sql语句执行失败 则调用全局异常处理类
            return ResponseResult.success(CodeMsg.SUCCESS); // 返回成功提示
        }
        return ResponseResult.fail(CodeMsg.USER_EXIT);
    }

    public ResponseResult login(LoginForm loginForm, HttpSession session) {

        // 从session里面获取验证码
        String code = (String) session.getAttribute("code");
        if(!code.equals(loginForm.getCode())){ // 验证码不正确
            return ResponseResult.fail(CodeMsg.CODE_ERROR);
        }
        User user = userMapper.getByUsername(loginForm.getUsername());// 根据表格中输入的用户名 调用查询方法
        if(ObjectUtils.isEmpty(user)){ // 输入的账号不存在
            return ResponseResult.fail(CodeMsg.USER_NOT_EXIT);
        }
        // 获取加密后的密码
        String md5 = SecureUtil.md5(loginForm.getPassword());
        String password = user.getPassword();
        if(!md5.equals(password)){ // 密码错误
            return ResponseResult.fail(CodeMsg.PASSWORD_ERROR);
        }
        // 登录成功 session里存user
        session.setAttribute("user",user);
        return ResponseResult.success(CodeMsg.LOGIN_SUCCESS);
    }


    public ResponseResult changePwd(HttpSession session, String oldPwd, String newPassword) {
        // 从session里面获取user
        User user1 = (User) session.getAttribute("user");
        String username = user1.getUsername();

        // 1.判定原始密码是否正确
        User user = userMapper.getByUsername(username);
        String password = user.getPassword();
        if(password.equals(SecureUtil.md5(oldPwd))){
            userMapper.changePwd(username,SecureUtil.md5(newPassword));

            return ResponseResult.success(CodeMsg.PASSWORD_CHANGE_SUCCESS);
        }else
            return ResponseResult.fail(CodeMsg.OLD_PASSWORD_ERROR);
    }



    public User getByUsername(String username) {
        User user = userMapper.getByUsername(username);
        return user;
    }


    public ResponseResult edit(User user) {
        userMapper.edit(user);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public ResponseResult add(User user) {
        // 判读用户是否存在
        User usr = userMapper.getByUsername(user.getUsername());
        if(!ObjectUtils.isEmpty(usr)){
           return ResponseResult.fail(CodeMsg.USER_EXIT);
        }
        // 设置默认密码
        user.setPassword(SecureUtil.md5("111111"));
        userMapper.add(user);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public ResponseResult batchDelete(Integer[] ids) {
        userMapper.batchDelete(ids); // 批量删除 出现异常 直接回滚 要么全部删除成功 要么全部失败
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public ResponseResult getPage(UserPageDTO userPageDTO){
        PageHelper.startPage(userPageDTO.getPage(),userPageDTO.getLimit());
        List<User> list = userMapper.getPage(userPageDTO);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);
        long total = userPageInfo.getTotal();
        List<User> list1 = userPageInfo.getList();
        return ResponseResult.success(CodeMsg.SUCCESS,list1,total);
    }


    public ResponseResult changePwd(String oldPwd, String newPassword, String username) {
        User user = userMapper.getByUsername(username);
        if (user.getPassword().equals(SecureUtil.md5(oldPwd))) {
            Integer line = userMapper.changePwd(username,SecureUtil.md5(newPassword));
            if (line == 1) {
                return ResponseResult.success(CodeMsg.SUCCESS);
            }else {
                return ResponseResult.fail(CodeMsg.ERROR);
            }
        }else {
            return ResponseResult.fail(CodeMsg.ERROR);
        }

    }


}
